package com.linkzd.app.service.impl;

import com.linkzd.app.mapper.DeviceInfoOfRoomMapper;
import com.linkzd.app.model.DeviceModel;
import com.linkzd.app.model.DeviceResult;
import com.linkzd.app.service.IDeviceInfoOfRoomService;
import com.linkzd.common.core.domain.AjaxResult;
import com.linkzd.common.core.domain.entity.SysDept;
import com.linkzd.common.core.domain.entity.SysUser;
import com.linkzd.common.utils.DateUtils;
import com.linkzd.common.utils.SecurityUtils;
import com.linkzd.iot.domain.DeviceInfo;
import com.linkzd.iot.domain.DeviceType;
import com.linkzd.iot.domain.RoomInfo;
import com.linkzd.iot.mapper.DeviceInfoMapper;
import com.linkzd.iot.mapper.IndexMapper;
import com.linkzd.iot.service.IDeviceInfoService;
import com.linkzd.iot.service.IDeviceTypeService;
import com.linkzd.iot.service.IRoomInfoService;
import com.linkzd.iot.service.Impl.DeviceInfoServiceImpl;
import com.linkzd.iot.utils.mqtt.client.MqttClientUtil;
import com.linkzd.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class DeviceInfoOfRoomServiceImpl implements IDeviceInfoOfRoomService
{
    @Autowired
    private DeviceInfoOfRoomMapper deviceInfoOfRoomMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private DeviceInfoOfRoomServiceImpl deviceInfoOfRoomService;

    @Autowired
    private IndexMapper indexMapper;

    private String getDeviceName(String type){
        String name = "";
        switch (type){
            case "10":
                name = "风扇";
                break;
            case "11":
                name = "LED灯";
                break;
            case "12":
                name = "加热器";
                break;
            case "13":
                name = "水泵";
                break;
            case "15":
                name = "光照值";
                break;
            case "16":
                name = "土壤湿度";
                break;
            case "17":
                name = "雨滴值";
                break;
        }
        return name;
    }
    /**
     * 查询设备信息列表
     * 
     * @param roomInfo 设备信息
     * @return 设备信息
     */
    @Override
    public DeviceResult selectDeviceInfoList(RoomInfo roomInfo)
    {
        List<DeviceInfo> deviceInfoList = deviceInfoOfRoomMapper.selectDeviceInfoList(roomInfo);
        List<DeviceModel> electionFanList = new ArrayList<>();
        List<DeviceModel> ledLightList = new ArrayList<>();
        String temperature = null;
        String humidness = null;
        for (DeviceInfo deviceInfo:deviceInfoList){
            String typeId = String.valueOf(deviceInfo.getType().getDeviceType());
            String deviceData = deviceInfo.getDeviceData();
            //温湿度解析
            if("14".equals(typeId)){
                String[] TH = deviceData.split(",");
                temperature = TH[0].split(":")[1];
                humidness = TH[1].split(":")[1];
            }else{
                DeviceModel deviceModel = new DeviceModel();
                deviceModel.setDeviceTypeId(deviceInfo.getDeviceTypeId());
                String deviceCode = deviceInfo.getDeviceCode();
                deviceModel.setDeviceId(deviceInfo.getDeviceId());
                deviceModel.setDeviceCode(deviceCode);
                deviceModel.setDeviceData(deviceData);
                deviceModel.setRoomNum(deviceInfo.getRoom().getRoomNum());

                String deviceName = getDeviceName(typeId);
                //deviceCode.substring(2)设备位置
                deviceName = deviceName + deviceCode.substring(5);
                deviceModel.setDeviceName(deviceName);
                if("10".equals(typeId)){//电风扇
                    electionFanList.add(deviceModel);
                }else if("11".equals(typeId)){//灯
                    ledLightList.add(deviceModel);
                }
            }
        }
        DeviceResult deviceResult = new DeviceResult(temperature,humidness,electionFanList,ledLightList);
//        System.out.println(deviceResult);
        return deviceResult;
    }
    /**
     * 大棚查询设备信息列表
     *
     * @param roomInfo 设备信息
     * @return 设备信息
     */
    @Override
    public com.linkzd.iot.model.DeviceResult selectDeviceInfoAllList(RoomInfo roomInfo)
    {
        List<DeviceInfo> deviceInfoList = indexMapper.selectDeviceInfoList(roomInfo);
        List<com.linkzd.iot.model.DeviceModel> actuator = new ArrayList<>();//执行器
        List<com.linkzd.iot.model.DeviceModel> sensor = new ArrayList<>();//传感器
        for (DeviceInfo deviceInfo:deviceInfoList){
            String typeId = String.valueOf(deviceInfo.getType().getDeviceVariety());
            String deviceData = deviceInfo.getDeviceData();
            String type = String.valueOf(deviceInfo.getType().getDeviceType());
            //装
            com.linkzd.iot.model.DeviceModel deviceModel = new com.linkzd.iot.model.DeviceModel();
            deviceModel.setDeviceTypeId(deviceInfo.getDeviceTypeId());
            String deviceCode = deviceInfo.getDeviceCode();
            deviceModel.setDeviceId(deviceInfo.getDeviceId());
            deviceModel.setDeviceCode(deviceCode);
            deviceModel.setRoomNum(deviceInfo.getRoom().getRoomNum());
            String deviceName = getDeviceName(type);
            deviceModel.setDeviceType(Long.valueOf(type));
            //传感器
            if("0".equals(typeId)){
                deviceModel.setDeviceName(deviceName);
                if("14".equals(type)){//温湿度
                    com.linkzd.iot.model.DeviceModel deviceModel2 = null;
                    try {
                        deviceModel2 = (com.linkzd.iot.model.DeviceModel) deviceModel.clone();
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                    String[] TH = deviceData.split(",");
                    String temperature = TH[0].split(":")[1];
                    String humidness = TH[1].split(":")[1];
                    deviceModel.setDeviceName("湿度");
                    deviceModel.setDeviceData(humidness);
                    deviceModel2.setDeviceName("温度");
                    deviceModel2.setDeviceData(temperature);
                    sensor.add(deviceModel2);
                }else {
                    String value = deviceData.split(":")[1];
                    deviceModel.setDeviceData(value);
                }

                sensor.add(deviceModel);
            }else if("1".equals(typeId)){//执行器
                //deviceCode.substring(2)设备位置
                deviceName = deviceName + deviceCode.substring(2);
                deviceModel.setDeviceName(deviceName);
                deviceModel.setDeviceData(deviceData);
                actuator.add(deviceModel);
            }
        }
        com.linkzd.iot.model.DeviceResult deviceResult = new com.linkzd.iot.model.DeviceResult(actuator,sensor);
        return deviceResult;
    }

    @Override
    public List<SysDept> selectRoomInfoList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptId = user.getDeptId();
        List<SysDept> sysDeptList = deptMapper.selectChildrenDeptById(deptId);
        SysDept sysDept1 = deptMapper.selectDeptById(deptId);
        sysDeptList.add(sysDept1);

        for (int i=0;i<sysDeptList.size();i++){
            List<SysDept> sysDepts = deptMapper.selectChildrenDeptById(sysDeptList.get(i).getDeptId());
            if(sysDepts.size() > 0){
                sysDeptList.remove(i);
                i--;
            }
        }
        return sysDeptList;
    }
}
